package cn.wolfcode.wms.controllrer;

import cn.wolfcode.wms.domain.SystemMenu;
import cn.wolfcode.wms.page.PageResult;
import cn.wolfcode.wms.qo.SystemMenuQueryObject;
import cn.wolfcode.wms.service.IPermissionService;
import cn.wolfcode.wms.service.ISystemMenuService;
import cn.wolfcode.wms.util.RequiredPermission;
import cn.wolfcode.wms.vo.JSONResult;
import cn.wolfcode.wms.vo.SystemMenuVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/systemMenu")
public class SystemMenuController {

    @Autowired
    private ISystemMenuService systemMenuservice ;

    @Autowired
    private IPermissionService permissionService;


    @RequestMapping("/list")
    @RequiredPermission("菜单列表")
    public String list(Model model ,@ModelAttribute("qo") SystemMenuQueryObject qo){

        if(qo.getParentId() != null){

            model.addAttribute("parents",systemMenuservice.getParents(qo.getParentId()));
        }
        PageResult pageResult = systemMenuservice.query(qo);

        model.addAttribute("pageResult",pageResult);
        return "systemMenu/list";
    }

    @RequiredPermission("菜单删除")
    @RequestMapping("/delete")
    @ResponseBody
    public JSONResult delete(Long id){
        try {
            systemMenuservice.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            return JSONResult.mark(e.getMessage());
        }

        return  JSONResult.success("删除成功");
    }

    @RequestMapping("/input")
    @RequiredPermission("菜单编辑")
    public String input(Model model,Long id,Long parentId){
        //查询父级菜单
        if(parentId != null){
            SystemMenu Menu = systemMenuservice.selectByPrimaryKey(parentId);
            model.addAttribute("parentName", Menu.getName());
            model.addAttribute("parentId", parentId);
        }else {
            model.addAttribute("parentName","根目录");
        }

        model.addAttribute("permissions",permissionService.selectAll());
        if(id != null){
            model.addAttribute("systemMenu",systemMenuservice.selectByPrimaryKey(id));
        }

        return "systemMenu/input";

    }


    @RequiredPermission("菜单添加或删除")
    @RequestMapping("/saveorupdate")
    @ResponseBody
    public JSONResult  saveorupdate(SystemMenu systemMenu){
        try {
            systemMenuservice.saveorupdate(systemMenu);
        }catch (Exception e){
            e.printStackTrace();
        }
        return JSONResult.success("操作成功!") ;
    }




    @RequestMapping("/loadMenuByParentSn")
    @ResponseBody
    public List<SystemMenuVO> loadMenuByParentSn(String parentSn){

        return systemMenuservice.loadMenuByParentSn(parentSn) ;

    }


}
















